﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SSRS_Plus
{
    public partial class frmCopyObjects : Form
    {
        public frmCopyObjects()
        {
            InitializeComponent();
        }

        private void frmCopyObjects_Load(object sender, EventArgs e)
        {
            LoadServers();
            btnCopy.Text += cmbServer1.Text;
        }

        private void LoadServers()
        {
            cmbServer1.Enabled = false;
            cmbServer1.Items.Clear();
            cmbServer2.Items.Clear();

            foreach (string str in ReportWrapper.AvailableServers.Keys)
            {
                cmbServer1.Items.Add(str);
                cmbServer2.Items.Add(str);
            }

            for (int i = 0; i < cmbServer1.Items.Count; i++)
            {
                if (cmbServer1.Items[i].ToString() == ReportWrapper.ActiveServer.ServerName)
                {
                    cmbServer1.SelectedIndex = i;
                    break;
                }
            }

            if (cmbServer2.Items.Count > 0)
                cmbServer2.SelectedIndex = 0;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            lstServer1.Nodes.Clear();

            foreach (Microsoft.SqlServer.ReportExecution.CatalogItem ct in ReportWrapper.GetServerObjects())
            {
                if (lstServer1.Nodes.Find(ct.Path.Replace(@"/" + ct.Name, ""), true).Count() > 0)
                {
                    lstServer1.Nodes.Find(ct.Path.Replace(@"/" + ct.Name, ""), true)[0].Nodes.Add(ct.Path, ct.Name);

                    lstServer1.Nodes.Find(ct.Path, true)[0].Tag = ct.Path;
                    lstServer1.Nodes.Find(ct.Path, true)[0].ToolTipText = ct.Type.ToString();

                    if (ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.DataSource)
                        lstServer1.Nodes.Find(ct.Path, true)[0].ImageIndex = 1;
                    else if (ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.Folder)
                        lstServer1.Nodes.Find(ct.Path, true)[0].ImageIndex = 2;
                    else if (ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.Report || ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.LinkedReport)
                        lstServer1.Nodes.Find(ct.Path, true)[0].ImageIndex = 3;
                }
                else
                {
                    TreeNode tr1 = lstServer1.Nodes.Add(ct.Path, ct.Name);
                    tr1.Tag = ct.Path;
                    tr1.ToolTipText = ct.Type.ToString();
                    tr1.ImageIndex = 2;
                }
            }
        }

        private void lstServer1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lstServer1.SelectedImageIndex = e.Node.ImageIndex;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
                ReportWrapper.ConnectSecondServer(cmbServer2.Text);
                lstDataSource.Items.Clear();
                lstServer2.Nodes.Clear();

                foreach (Microsoft.SqlServer.ReportExecution.CatalogItem ct in ReportWrapper.GetSecondServerObjects())
                {
                    if (ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.DataSource)
                    {
                        lstDataSource.Items.Add(ct.Path);
                    }

                    if (lstServer2.Nodes.Find(ct.Path.Replace(@"/" + ct.Name, ""), true).Count() > 0)
                    {
                        lstServer2.Nodes.Find(ct.Path.Replace(@"/" + ct.Name, ""), true)[0].Nodes.Add(ct.Path, ct.Name);

                        lstServer2.Nodes.Find(ct.Path, true)[0].Tag = ct.Path;
                        lstServer2.Nodes.Find(ct.Path, true)[0].ToolTipText = ct.Type.ToString();

                        if (ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.DataSource)
                            lstServer2.Nodes.Find(ct.Path, true)[0].ImageIndex = 1;
                        else if (ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.Folder)
                            lstServer2.Nodes.Find(ct.Path, true)[0].ImageIndex = 2;
                        else if (ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.Report || ct.Type == Microsoft.SqlServer.ReportExecution.ItemTypeEnum.LinkedReport)
                            lstServer2.Nodes.Find(ct.Path, true)[0].ImageIndex = 3;
                    }
                    else
                    {
                        TreeNode tr1 = lstServer2.Nodes.Add(ct.Path, ct.Name);
                        tr1.Tag = ct.Path;
                        tr1.ToolTipText = ct.Type.ToString();
                        tr1.ImageIndex = 2;
                    }
                }

                this.Cursor = System.Windows.Forms.Cursors.Arrow;
            }
            catch
            {
                MessageBox.Show("Unable to connect to specified Server!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Cursor = System.Windows.Forms.Cursors.Arrow;
            }
        }

        private void lstServer2_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lstServer2.SelectedImageIndex = e.Node.ImageIndex;
            lblItem.Text = "Selected Item: " + e.Node.Tag.ToString();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (lstServer2.SelectedNode != null && lstServer2.SelectedNode.ToolTipText == "Folder")
            {
                foreach (TreeNode tn in lstServer1.Nodes)
                {
                    CopyReportsRecursive(tn);
                }
                MessageBox.Show("Report(s) copied successfully to "+cmbServer2.Text, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                MessageBox.Show("Please select Folder from "+cmbServer2.Text, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void CopyReportsRecursive(TreeNode trNode)
        {
            foreach (TreeNode tn in trNode.Nodes)
            {
                if ((tn.ToolTipText == "Report" || tn.ToolTipText == "LinkedReport") && tn.Checked)
                {
                    ReportWrapper.CopyItem(tn.Tag.ToString(), tn.Text, lstServer2.SelectedNode.Tag.ToString());
                }
            }

            foreach (TreeNode tn in trNode.Nodes)
            {
                CopyReportsRecursive(tn);
            }
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstServer2.Nodes.Count > 0 && lstDataSource.Text != "")
                {
                    foreach (TreeNode tr in lstServer2.Nodes)
                    {
                        SetDataSourceRecursive(tr);
                    }

                    MessageBox.Show("Data Source is set to selected report(s)!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SetDataSourceRecursive(TreeNode trNode)
        {
            foreach (TreeNode tn in trNode.Nodes)
            {
                if ((tn.ToolTipText == "Report" && tn.Checked))
                {
                    ReportWrapper.SetDataSource(tn.Tag.ToString(), lstDataSource.Text);
                }
            }

            foreach (TreeNode tn in trNode.Nodes)
            {
                SetDataSourceRecursive(tn);
            }
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void lstServer1_DoubleClick(object sender, EventArgs e)
        {
            if (lstServer1.SelectedNode != null && lstServer1.SelectedNode.ToolTipText.Contains("Report"))
            {
                string reportLink = ReportWrapper.ActiveServer.ServerURL.ToString().Substring(1, ReportWrapper.ActiveServer.ServerURL.ToString().LastIndexOf("/") - 1);
                reportLink = "h" + reportLink.ToUpper().Replace("REPORTSERVER", "REPORTS").ToLower() + "/Pages/Report.aspx?ItemPath=";
                reportLink = reportLink + lstServer1.SelectedNode.Tag.ToString().Replace("/", "%2f").Replace(" ", "+");

                System.Diagnostics.Process.Start(reportLink);
            }
        }

        private void lstServer2_DoubleClick(object sender, EventArgs e)
        {
            if (lstServer2.SelectedNode != null && lstServer2.SelectedNode.ToolTipText.Contains("Report"))
            {
                string reportLink = ReportWrapper.SecondActiveServer.ServerURL.ToString().Substring(1, ReportWrapper.SecondActiveServer.ServerURL.ToString().LastIndexOf("/") - 1);
                reportLink = "h" + reportLink.ToUpper().Replace("REPORTSERVER", "REPORTS").ToLower() + "/Pages/Report.aspx?ItemPath=";
                reportLink = reportLink + lstServer2.SelectedNode.Tag.ToString().Replace("/", "%2f").Replace(" ", "+");

                System.Diagnostics.Process.Start(reportLink);
            }
        }

    }
}
